<?php
	echo "<h2 id='contentTitle'>".$pageTitle."</h2>";
	echo $this->renderElement('message');

	if ($othAuth->hasPermission('employees/add/')) {
		echo '<div> Cadastre os funcionários da sua entidade.<br><br> </div>';

		$addButton = "<span class=\"buttonAdd\">Cadastrar funcionário</span>";
		echo html_entity_decode($html->link($addButton, '/employees/add/',array('class'=>'button')));
		
		$inviteButton = "<span class=\"buttonAdd\">Convidar funcionário</span>";
		echo html_entity_decode($html->link($inviteButton, '/employees/invite/',array('class'=>'button')));
	}

	if (isset($employees) && is_array($employees) && (!empty($employees))) {
		echo "<br />";
		$unitUser = $othAuth->getData("unit");
		$group = $othAuth->getData('group');
		$level =$group['level'];

		echo "<table>";

		/* Cabeçalho */
		echo "<thead>";
		echo "<div></div>";
		echo "<tr class='tableHeader'>";
		echo "<th class='tableHeaderBegin'>Nome</th>";
		echo "<th class='tableHeaderMiddle'>Unidade</th>";
		echo "<th class='tableHeaderMiddle'>Permissão</th>";
		if ($othAuth->hasPermission('employees/edit/')) {
			echo "<th class='tableHeaderMiddle'>Editar</th>";
		}
		if ($othAuth->hasPermission('employees/delete/')) {
			echo "<th class='tableHeaderEnd'>Excluir</th>";
		}
		echo "</tr>";
		echo "</thead>";

		/* Rodapé */
		echo "<tfoot>";
		echo "<tr class='tableFooter'>
		<td colspan='100%'></td>
		</tr>";
		echo "</tfoot>";

		$i=0;
		foreach ($employees as $employee) {
			if (($i++ % 2) == 1) $zebrado = " class='oddTr' ";
			else $zebrado = "";
			echo "<tr ".$zebrado.">";
			echo "<td>".$html->link($employee['User']['name'],'/employees/view/'.$employee['Employee']['id'])."</td>";
			echo "<td>";
			foreach($employee['User']['Unit'] as $unit) {
				echo $html->link($unit['nome'],'/units/view/'.$unit['id'])."<br>";
			}
			echo "</td>";

			echo "<td><small>".$employee['User']['Group']['name']."</small></td>";
			// TODO: listar todas as unidades do funcionario

			if ($othAuth->hasPermission('employees/edit')) {
				echo "<td class='tdCenter'>".html_entity_decode($html->link($html->image('fileedit.png',array('class'=>'icon')), '/employees/edit/'.$employee['Employee']['id']))."</td>";
			}
			if ($othAuth->hasPermission('employees/delete')) {
				echo "<td class='tdCenter'>".html_entity_decode($html->link($html->image('filedel.png',array('class'=>'icon')), '/employees/delete/'.$employee['Employee']['id'], array(), 'Confirma a exclusão deste item?'))."</td>";
			}
			echo "</tr>";
		}
		echo "</table>";
	} else {
		echo "<p>Não há nenhum funcionário cadastrado.</p>";
	}